package com.pocket.insight.base.listener;

import com.pocket.insight.base.sql.context.JDBCConfigContext;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * 描述：
 *
 * @author shuize
 * @create 2024/12/1
 */
@Slf4j
@Component
public class InitListener implements ApplicationListener<ApplicationReadyEvent> {

    @Value("${systemConfig.jdbcConfig:}")
    private String jdbcConfig;

    @SneakyThrows
    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        if (StringUtils.isNotBlank(jdbcConfig)) {
            log.info("开始初始化 JDBC 配置项：JDBCConfigContext");
            JDBCConfigContext.init(new String(Files.readAllBytes(Paths.get(jdbcConfig)), StandardCharsets.UTF_8));
        }
    }
}
